package com.ella;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.ella.entity.SysMenu;
import com.ella.entity.SysUser;
import com.ella.utils.JwtUtil;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.lang.reflect.Field;
import java.util.*;
import java.util.function.Consumer;

public class codeGenerator {

	/**
	 * 数据源配置
	 */
	private static final DataSourceConfig DATA_SOURCE_CONFIG = new DataSourceConfig
			.Builder("jdbc:mysql://xxxx:3306/baomidou?serverTimezone=Asia/Shanghai", "root", "123456")
			.schema("baomidou")
			.build();

	@Test
	public void StringUtils() {
		/**
		 * TODO empty是为初始化
		 * TODO blank可能未初始化了或者初始化了是空字符串
		 */

		System.out.println(StringUtils.isEmpty(null));//true

		System.out.println(StringUtils.isEmpty("null"));//false

		System.out.println(StringUtils.isEmpty(" "));//false

		System.out.println(StringUtils.isBlank(null));//true

		System.out.println(StringUtils.isBlank(""));//true

		System.out.println(StringUtils.isBlank("  		"));//true


		ArrayList<String> list = new ArrayList<>();

		Collections.addAll(list,"demo1","demo2","demo3");

		list.forEach(new Consumer<String>() {
			@Override
			public void accept(String item) {
				if (item.equals("demo2")){
						System.out.println("success");      //
						return;
					}
					System.out.println("failure -- 001");
			}
		});
		String s = Objects.requireNonNull(null);

	}

	@Test
	public void TokenJsonCreate() throws IllegalAccessException {

		byte[] encodedKey = Base64.getDecoder().decode(JwtUtil.JWT_KEY);
		SecretKey key = new SecretKeySpec(encodedKey, 0, encodedKey.length, "RSA");

		JwtBuilder jwtBuilder = Jwts.builder()
				.setId(UUID.randomUUID().toString())              //唯一的ID
				.setSubject("admin")     // 主题  可以是JSON数据
				.setIssuer("yuhao")        // 签发者
				.setIssuedAt(new Date(System.currentTimeMillis()))      // 签发时间
				.signWith(SignatureAlgorithm.HS256, key) //使用HS256对称加密算法签名, 第二个参数为秘钥
				.setExpiration(new Date(System.currentTimeMillis() + 1500L));
		String compact = jwtBuilder.compact();
		System.out.println(compact);




		/*try {
			Claims claims = JwtUtil.parseJWT("eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIzNjBlYWY1YWNiNTQ0NWE3YmE0ODQxMDdjNjcxYjkyYiIsInN1YiI6IntcInBlcm1pc3Npb25zXCI6W1wic3lzdGVtOnVzZXI6cmVzZXRQd2RcIixcInN5c3RlbTpwb3N0Omxpc3RcIixcIm1vbml0b3I6b3BlcmxvZzpleHBvcnRcIixcIm1vbml0b3I6ZHJ1aWQ6bGlzdFwiLFwic3lzdGVtOm1lbnU6cXVlcnlcIixcInN5c3RlbTpkZXB0OnJlbW92ZVwiLFwic3lzdGVtOm1lbnU6bGlzdFwiLFwidG9vbDpnZW46ZWRpdFwiLFwic3lzdGVtOmRpY3Q6ZWRpdFwiLFwibW9uaXRvcjpsb2dpbmluZm9yOnJlbW92ZVwiLFwibW9uaXRvcjpqb2I6bGlzdFwiLFwic3lzdGVtOnVzZXI6cXVlcnlcIixcInN5c3RlbTp1c2VyOmFkZFwiLFwic3lzdGVtOm5vdGljZTpyZW1vdmVcIixcInN5c3RlbTp1c2VyOmV4cG9ydFwiLFwic3lzdGVtOnJvbGU6cmVtb3ZlXCIsXCJtb25pdG9yOmpvYjplZGl0XCIsXCJ0b29sOmdlbjpxdWVyeVwiLFwic3lzdGVtOmRlcHQ6cXVlcnlcIixcInN5c3RlbTpkaWN0Omxpc3RcIixcIm1vbml0b3I6am9iOnF1ZXJ5XCIsXCJtb25pdG9yOm9ubGluZTpmb3JjZUxvZ291dFwiLFwic3lzdGVtOm5vdGljZTpsaXN0XCIsXCJzeXN0ZW06ZGljdDpxdWVyeVwiLFwibW9uaX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IwXCIsXCJ1cGRhdGVCeVwiOlwiXCIsXCJ1c2VySWRcIjoxLFwidXNlck5hbWVcIjpcIuiLpeS-nVwiLFwidXNlclR5cGVcIjpcIjAwXCJ9LFwidXNlcklkXCI6MSxcInVzZXJuYW1lXCI6XCLoi6Xkvp1cIn0iLCJpc3MiOiJzZyIsImlhdCI6MTY4NDc1OTc5NCwiZXhwIjoxNjg0NzYzMzk0fQ.kl8Quks7P6grw1XAr5iD1FC_SUEOYIfwXWIzvZU_Kis");
			String subject = claims.getSubject();
			LoginUser loginUser = JSONObject.parseObject(subject, LoginUser.class);
			System.out.println(loginUser);
		} catch (Exception exception) {
			exception.printStackTrace();
		}*/

	}

	/*	@Test
	public void generate(){
		// 1、创建代码生成器
		AutoGenerator mpg = new AutoGenerator();

		// 2、全局配置
		GlobalConfig gc = new GlobalConfig();
		String projectPath = System.getProperty("user.dir");
		System.out.println(projectPath);
		gc.setOutputDir("D:\\EllaSecondHandlerPlatFrom" + "/src/main/java");
		gc.setAuthor("浮生若梦");
		gc.setOpen(false); //生成后是否打开资源管理器
		gc.setFileOverride(false); //重新生成时文件是否覆盖

		 * mp生成service层代码，默认接口名称第一个字母有 I
		 * UcenterService

		gc.setServiceName("%sService");	//去掉Service接口的首字母I
		gc.setIdType(IdType.ID_WORKER);
		gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
		gc.setSwagger2(true);// todo 开启Swagger2模式

		mpg.setGlobalConfig(gc);

		// 3、数据源配置
		DataSourceConfig dsc = new DataSourceConfig();
		dsc.setUrl("jdbc:mysql://localhost:3306/fushen?serverTimezone=GMT%2B8");
		dsc.setDriverName("com.mysql.cj.jdbc.Driver");
		dsc.setUsername("root");
		dsc.setPassword("123456");
		dsc.setDbType(DbType.MYSQL);
		mpg.setDataSource(dsc);

		// 4、包配置
		PackageConfig pc = new PackageConfig();
		pc.setParent("com.ella");
		pc.setController("controller");
		pc.setEntity("entity");
		pc.setService("service");
		pc.setMapper("mapper");
		mpg.setPackageInfo(pc);

		// 5、策略配置
		StrategyConfig strategy = new StrategyConfig();
		strategy.setInclude("sys_user");
		strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
		strategy.setTablePrefix( "_"); //生成实体时去掉表前缀

		strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略
		strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作

		strategy.setRestControllerStyle(true); //restful api风格控制器
		strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符

		mpg.setStrategy(strategy);
		// 6、执行
		mpg.execute();
	}*/


	@Test
	public void generate(){

		//String moduleName = "";     单体项目
		//String moduleName = "Wish-Cloud-master/wish-modules/wish-contract"; //  多模块项目示例
		String projectPath = System.getProperty("user.dir");
		String databaseName = "ella";
		FastAutoGenerator.create(
				"jdbc:mysql://127.0.0.1:3306/" + databaseName + "?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai",
				"root", "123456")
				// 全局配置
				.globalConfig((scanner, builder) -> builder.outputDir(projectPath + "/src/main/java")
										.author("浮生若梦").fileOverride().enableSwagger())
				// 包配置
				.packageConfig((scanner, builder) -> builder
				.parent("com.ella")
				.pathInfo(Collections.singletonMap(OutputFile.xml, //曾经是mapperXml
						projectPath  +"/src/main/resources/com/ella/mapper")))
				// 策略配置
				.strategyConfig((scanner, builder) -> builder
				.addInclude("sys_menu")
                 //.addTablePrefix("_") //过滤表前缀
				.controllerBuilder()        //  生成@RestController控制器
				.enableRestStyle()
				.enableHyphenStyle()        //  开启Controller驼峰转连字符
				.entityBuilder()
				.enableLombok()             //  实体是否为lombok模型
				.naming(NamingStrategy.underline_to_camel) // 数据库表字段映射到实体类的命名策略
				.columnNaming(NamingStrategy.underline_to_camel)//  数据库表字段映射到实体类字段的命名策略
				.build())
				.execute();
	}

	@Test
	public void name() {
		String projectPath = System.getProperty("user.dir");
		System.out.println(projectPath);
	}

	// 处理 all 情况
	protected static List<String> getTables(String tables) {
		return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
	}
}
